
/****
 * Copyright (C) 2006 dNux Team
 *                                                            ****
 * This program is free software; you can redistribute it and/or *
 * modify under the terms of the GNU General Public License as   *
 * published by the Free Software Foundation;                    *
 * either version 2 of the License.                              *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of without even the implied waranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.          *
 * See the GNU General Public License for more details.          *
 *                                                               *
 * You should have received a copy of the                        *
 * GNU General Public License                                    *
 * along with this program; if not, write to the                 *
 * Free Software Foundation, Inc.,                               *
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. *
 *                                                               *
 ****                                                         ****/

#include "dcore.h"

static void d_cmdline_display_help(const d_cmdline_t *cmdline);

void
d_cmdline_parse(const d_cmdline_t *cmdline, int argc, char **argv)
{
  d_bool_t found;
  unsigned int i,j;

  for(i=0; i < argc; i++) {
    found=d_false;
    if ((! strcmp(argv[i], "-h")) || (! strcmp(argv[i], "--help"))) {
      d_cmdline_display_help(cmdline);
    }
    for(j=0; cmdline[j].short_name; j++) {
      if (cmdline[j].long_name) if (! strcmp(cmdline[j].long_name, argv[i])) {
        found=d_true;
      }
      if (cmdline[j].short_name) if (! strcmp(cmdline[j].short_name, argv[i])) {
        found=d_true;
      }
      if (found) {
        switch(cmdline[j].arg_type) {
          case D_CMDLINE_ARG_NONE:
	    *((d_bool_t*)cmdline[j].arg_var)=d_true;
	    break;
	  case D_CMDLINE_ARG_INT:
	    break;
	  case D_CMDLINE_ARG_STRING:
	    if ( i + 1 < argc) {
              *((char**)cmdline[j].arg_var) = argv[++i];
	    }
	    break;
	}
      }
    }
  }
}

static void
d_cmdline_display_help(const d_cmdline_t * cmdline)
{
  unsigned int i;
  for(i=0; cmdline[i].short_name; i++) {
    fprintf(stderr, "%8s %4s %12s\n", cmdline[i].short_name, cmdline[i].long_name, cmdline[i].desc);
  }
  exit(EXIT_SUCCESS);
}

